package com.sams.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sams.entity.School;
import com.sams.entity.SpotUpload;
import com.sams.entity.SpotUploadExample;
import com.sams.entity.SpotUploadExample.Criteria;
import com.sams.entity.User;
import com.sams.mapper.SchoolMapper;
import com.sams.mapper.SpotUploadMapper;
import com.sams.service.SpotUploadService;

@Service
public class SpotUploadServiceImpl implements SpotUploadService{
	
	@Autowired
	private SpotUploadMapper mapper;
	@Autowired
	private SchoolMapper schoolMapper;
	
	public PageInfo<SpotUpload> ListSpot(int currentPage, Integer schid, Integer status, User user,Integer zid) {
	
		SpotUploadExample spotUploadExample = new SpotUploadExample();
		Criteria createCriteria = spotUploadExample.createCriteria();
		// 1.roleid=1校级用户 uid=1且 查询的status=1，2，3的数据
		if (user.getRoleid() == 1) {
			createCriteria.andUidEqualTo(user.getId());
			createCriteria.andStatusNotEqualTo(4);
		} else if (user.getRoleid() == 2) {
			// 2.roledid=2镇级用户 查镇下面的学校 再查status1，2，3
			List<Integer> scIds = new ArrayList<Integer>();
			List<School> scList = schoolMapper.selectSchoolByUid(user.getId());
			for (School sc : scList) {
				scIds.add(sc.getSchid());
			}
			createCriteria.andSchidIn(scIds);
			/* createCriteria.andStatusNotEqualTo(4); */
			createCriteria.andStatusEqualTo(2); //只需要它审核的数据  

		} else {
			//， 3：县账号，4 ：管理员
			 if(user.getRoleid()==3) {// 3//镇账户查询状态： 县审核重点3  //只需要它审核的数据    其他县不考虑
				createCriteria.andStatusEqualTo(3);
			}
			 //根据镇id 也是查询所有学校
			 if(zid!=null &&  !zid.equals("")) {
				 List<Integer> scIds = new ArrayList<Integer>();
				 List<School> scList = schoolMapper.selectSchoolByZid(zid);
				 for(School sc : scList) {
					 scIds.add(sc.getSchid());
				 }
				 createCriteria.andSchidIn(scIds);
			 }
		}
		// 按照条件查询
		//按照条件查询
		if(schid!=null && !"".equals(schid)) {
			createCriteria.andSchidEqualTo(schid);
		}
		if(status!=null && !"".equals(status)) {
			createCriteria.andStatusEqualTo(status);
		}
		// 查询不等于4的
		createCriteria.andStatusNotEqualTo(4);
		//设置分页
		PageHelper.startPage(currentPage, 20);
		List<SpotUpload> selectByExample = mapper.selectByExample(spotUploadExample);
		PageInfo<SpotUpload> pageInfo=new PageInfo<SpotUpload>(selectByExample);
		return pageInfo;
	}

	public int add(SpotUpload spot) {
		
		
		return mapper.insert(spot);
	}

	public int delete(int id) {
		// TODO Auto-generated method stub
		SpotUpload spot = mapper.selectByPrimaryKey(id);
		if(spot.getStatus()==1) {
			return mapper.deleteByPrimaryKey(id);
		}
		return 0;
				
	}

	public SpotUpload findById(int id) {
		// TODO Auto-generated method stub
		return mapper.selectByPrimaryKey(id);
	}

	public int update(SpotUpload spot) {
		// TODO Auto-generated method stub
		return mapper.updateByPrimaryKey(spot);
	}

	public int report(Integer id) {
		return mapper.report(id);
 	}
	
	public int noreport(Integer id) {
		return mapper.noreport(id);
 	}

}
